package rev2;

import java.util.ArrayList;
import java.util.HashMap;

import rev1.DHTUtil;

public class NodeMyString {

	int id;
	HashMap<String, MyString> map;
	ArrayList<NodeMyString> fingers;
	
	
	public NodeMyString(int id)
	{
		this.id = id;
		map =  new HashMap<String, MyString>();
		fingers = new ArrayList<NodeMyString>();
	}
	
	
	public MyString get(String hsh) 
	{
		String sNodeHsh = DHTUtil.parseNodeHash(hsh);
		String sKey = DHTUtil.parseKey(hsh);
		
		if (sNodeHsh == null || sKey == null)
			throw new RuntimeException("Bad hsh in-parm");
		
		int inNodeHsh = Integer.parseInt(sNodeHsh);

		NodeMyString prevNode = fingers.get(0);
		if (id <= inNodeHsh && inNodeHsh < prevNode.id)
			return map.get(sKey);
		
		for (int i = 1; i < fingers.size(); ++i) {
			NodeMyString nextNode = fingers.get(i);
			
			if (inNodeHsh >= prevNode.id && inNodeHsh < nextNode.id)
				return prevNode.get(hsh);			
			
			prevNode = nextNode;
		}
		
		return null; // ???? TODO - fix this
	}

}
